package com.supermarket.manage.provider.instock.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.supermarket.manage.common.constant.StatusConstant;
import com.supermarket.manage.common.pojo.Supplier;
import com.supermarket.manage.provider.api.instock.SupplierManageService;
import com.supermarket.manage.provider.instock.mapper.SupplierMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-03-13 18:00
 **/
@Service(version = "1.0.0")
public class SupplierManageServiceImpl implements SupplierManageService {

    @Resource
    private SupplierMapper supplierMapper;

    @Override
    public Integer getCountBySupplier(Supplier supplier) {
        QueryWrapper<Supplier> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StatusConstant.USE)
                .eq("buyer_id", supplier.getBuyerId())
                .eq("sphone", supplier.getSphone())
                .eq("sname", supplier.getSname());
        return supplierMapper.selectCount(wrapper);
    }

    @Override
    public Boolean addSupplier(Supplier supplier) {
        Date date = new Date();
        supplier.setStatus(StatusConstant.USE);
        supplier.setCreated(date);
        supplier.setUpdated(date);
        return supplierMapper.insert(supplier) == 1;
    }

    @Override
    public Boolean deleteSupplier(String sid) {
        Supplier supplier = new Supplier();
        supplier.setSid(sid);
        supplier.setStatus(StatusConstant.DELETED);
        supplier.setUpdated(new Date());
        return supplierMapper.updateById(supplier) == 1;
    }

    @Override
    public Boolean updateSupplier(Supplier supplier) {
        supplier.setUpdated(new Date());
        return supplierMapper.updateById(supplier) == 1;
    }

    @Override
    public Supplier getSupplier(String sid) {
        return supplierMapper.selectById(sid);
    }

    @Override
    public List<Supplier> getSupplierList(Supplier supplier) {
        QueryWrapper<Supplier> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StatusConstant.USE)
                .eq("buyer_id", supplier.getBuyerId());
        if (StringUtils.isNotBlank(supplier.getSname())) {
            wrapper.like("sname", supplier.getSname());
        }
        if (StringUtils.isNotBlank(supplier.getSphone())) {
            wrapper.like("sphone", supplier.getSphone());
        }
        wrapper.orderByDesc("created");
        return supplierMapper.selectList(wrapper);
    }
}
